package com.hb.domain.sys.service.impl;

import com.hb.application.cache.SysDictCache;
import com.hb.domain.sys.assembler.DictDOConvert;
import com.hb.domain.sys.service.ISysDictService;
import com.hb.repository.SysDictRepository;
import com.hb.repository.entity.SysDict;
import com.hb.vo.DictVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @projectName: zhb-frame-build
 * @author: zhouhaizhi
 * @createTime: 2021/8/26 17:24
 * @version:1.0
 */
@Service
public class SysDictServiceImpl implements ISysDictService {

    @Autowired
    private SysDictCache sysDictCache;

    @Autowired
    private SysDictRepository sysDictRepository;

    @Autowired
    private DictDOConvert dictDOConvert;

    @Override
    public DictVO getDictByCode(Integer dictType, String dictCode) {
        DictVO dictVo = sysDictCache.getDict(dictType, dictCode);
        if (null == dictVo || StringUtils.isBlank(dictVo.getDictCode())) {
            SysDict sysDict = sysDictRepository.getDictByCode(dictType, dictCode);
            dictVo = dictDOConvert.toVo(sysDict);

            sysDictCache.putDict(dictVo);
        }
        return dictVo;
    }

    @Override
    public List<DictVO> getListDictByType(Integer dictType) {
        List<DictVO> dictVOS = sysDictCache.getListDict(dictType);
        if (CollectionUtils.isEmpty(dictVOS)) {
            List<SysDict> sysDicts = sysDictRepository.getListDictByType(dictType);
            if (CollectionUtils.isNotEmpty(sysDicts)) {
                dictVOS = dictDOConvert.toVoList(sysDicts);
                sysDictCache.putListDict(dictType, dictVOS);
            }
        }

        return dictVOS;
    }
}
